export function toBase64(path){
	console.log(path);
	// let blob = path.slice(5);
	// console.log(blob);
	// #ifdef H5
	console.log('h5');
	var cimg={};
	uni.getImageInfo({
		src:path,
		success: (res) => {
			// console.log(res);
			cimg=res;
		}
	})				
	return new Promise((resolve,reject)=>{
		setTimeout(()=>{
		var cimage = new Image();
		cimage.setAttribute('crossOrigin','Anoymous');
		cimage.src = path;
		cimage.onload = function(){
			let canvas = document.createElement('canvas');
			canvas.width=cimg.width;
			canvas.height=cimg.height;
			canvas.getContext('2d').drawImage(cimage,0,0);
			let result = canvas.toDataURL('image/png')
			// console.log(result);
			resolve(result);
		}
		},50)
	})	
	// #endif
	// #ifdef APP
	console.log('app');
	return new Promise(function(resolve,reject){		
		plus.io.resolveLocalFileSystemURL(path,function(entry){
			// console.log(entry);
			entry.file(function(file){
				// console.log(file);
				var fileReader = new plus.io.FileReader();
				fileReader.onload = function(w){
					// console.log(w.target.result);
					resolve(w.target.result)
				}
				fileReader.onerror = function(error) {
				    reject(error)
				}
				fileReader.readAsDataURL(file)
			})
		})
	})
	// #endif
	// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ ||QUICKAPP-WEBVIEW-HUAWEI ||APP-HARMONY ||MP-HARMONY
	console.log('wx more');
	return new Promise(function(resolve,reject){
		uni.getFileSystemManager().readFile({
			filePath:path,
			encoding:'base64',
			success: (res) => {
				// console.log(res);
				var base64 = 'data:image/png;base64,' + res.data
				resolve(base64)
			},
			fail:(err) =>{
				reject(err)
			}
		})
	})
	// #endif
}

export function tominBase64(pathmin,ratio){
	console.log(pathmin,ratio);
			// #ifdef H5
			console.log('该接口不支持h5，请使用toBase64()');
			return new Promise(function(resolve,reject){
				reject({errMsg:'该接口不支持h5，请使用toBase64()'})
			})
			// #endif
			// #ifdef APP
			console.log('appmin');
			return new Promise(function(resolve,reject){
				uni.compressImage({
					src:pathmin,
					quality:60,
					width:ratio?ratio+'%':'auto',
					success: (res) => {
						// console.log(res);
						plus.io.resolveLocalFileSystemURL(res.tempFilePath,function(entry){
							// console.log(entry);
							entry.file(function(file){
								// console.log(file);
								var fileReader = new plus.io.FileReader();
								fileReader.onload = function(w){
									// console.log(w.target.result);
									resolve(w.target.result)
								}
								fileReader.onerror = function(error) {
									reject(error)
								}
								fileReader.readAsDataURL(file)
							})
						})
					},fail: (err) => {
						reject(err)
					}
				})				
			})
			// #endif
			// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ ||QUICKAPP-WEBVIEW-HUAWEI ||APP-HARMONY ||MP-HARMONY
			console.log('wx more min');
			return new Promise(function(resolve,reject){
				uni.compressImage({
					src:pathmin,
					quality:60,
					width:ratio?ratio+'%':'auto',
					success: (res) => {
						uni.getFileSystemManager().readFile({
							filePath:res.tempFilePath,
							encoding:'base64',
							success: (res) => {
								// console.log(res);
								var base64 = 'data:image/png;base64,' + res.data
								resolve(base64)
							},
							fail:(err) =>{
								reject(err)
							}
						})
					},fail: (err) => {
						reject(err)
					}
				})
			})
			// #endif
}